package services;

import java.util.List;

import models.dao.CommonDAO;
import models.dao.ModuleDAO;
import models.dao.ModuleResourceDAO;
import models.dao.ModuleStaffDAO;
import models.dao.ModuleUriDAO;
import models.dao.StaffDAO;
import models.entities.Module;
import models.entities.ModuleResource;
import models.entities.ModuleStaff;
import models.entities.ModuleUri;
import models.entities.Staff;
import models.enumtype.ActiveType;
import models.enumtype.KPaging;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ModuleMgr {
	
	@Autowired
	ModuleDAO moduleDAO;
	
	@Autowired
	ModuleResourceDAO moduleResourceDAO;
	
	@Autowired
	ModuleUriDAO moduleUriDAO;
	
	@Autowired
	ModuleStaffDAO moduleStaffDAO;
	
	@Autowired
	StaffDAO staffDAO;

	@Transactional(rollbackFor = Exception.class)
	public void installModule(Module module, List<ModuleResource> lstModuleResource, List<ModuleUri> lstModuleUri) throws Exception {
		module = moduleDAO.createModule(module);
		for(ModuleResource mr : lstModuleResource) {
			moduleResourceDAO.createModuleResource(mr);
		}
		for(ModuleUri mu : lstModuleUri) {
			moduleUriDAO.createModule(mu);
		}
	}
	
	public List<Module> getListModule(KPaging<Module> kPaging, String moduleCode, String moduleName, ActiveType status) throws Exception {
		return moduleDAO.getListModule(kPaging, moduleCode, moduleName, status);
	}
	
	public List<ModuleStaff> getListModuleStaff(KPaging<ModuleStaff> kPaging, Long moduleId) throws Exception {
		return moduleStaffDAO.getListModuleStaff(kPaging, moduleId);
	}
	
	public List<Staff> getListStaff(KPaging<Staff> kPaging, String staffCode, String staffName, ActiveType status) throws Exception {
		return staffDAO.getListStaff(kPaging, staffCode, staffName, status);
	}
	
	public ModuleStaff getModuleStaffByModuleIdStaffId(Long moduleId, Long staffId) throws Exception {
		return moduleStaffDAO.getModuleStaffByModuleIdStaffId(moduleId, staffId);
	}
	
	public void deleteModuleStaff(ModuleStaff moduleStaff) throws Exception {
		moduleStaffDAO.deleteModule(moduleStaff);
	}
}
